package com.nosnow.gmall.manage.controller;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @类名称：
 * @类描述：
 * @author: bingHeng
 * @Date
 * @Version: 1.0
*/
@RestController
public class FileUploadController {

    // 服务器图片地址
    private String url = "http://192.168.168.130";

    /**
     * 说明：测试文件上传
     * MultipartFile 接收一个文件上传的列表
     * @param file
     * @return
     */
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    public String fileUpload(@RequestParam("file")MultipartFile file) {
        String fileUrl = "";
        if(file != null) {
            System.out.println("multipartFile = " + file.getName() + "|" + file.getSize());
        }
        try {
            String conf = this.getClass().getResource("/tracker.conf").getFile();
            ClientGlobal.init(conf);
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            String[] upload_file = storageClient.upload_file(file.getBytes(), "jpg", null);
            for(int i = 0; i < upload_file.length; i++) {
                String s = upload_file[i];
                System.out.println("s = " + s);
                fileUrl += "/" + s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url + fileUrl;
    }
}
